<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/5/6
 * Time: 12:31
 */

namespace App\Http\Classes;


use Illuminate\Support\Facades\DB;

class Log
{
    public static function userError($event,$code,$message,$file,$line,$user){
        $log = DB::table('weibo_buglogs');
        $log = $log->insert([
            'log_code' => $code,
            'log_user' => $user,
            'log_message' => $message,
            'log_file' => $file,
            'log_line' => $line,
            'log_event' => $event,
            'log_ip' => $_SERVER["REMOTE_ADDR"]
        ]);
        unset($log);
    }

    public static function adminError($event,$code,$message,$file,$line,$admin){
        $log = DB::table('weibo_buglogs');
        $log = $log->insert([
            'log_code' => $code,
            'log_admin' => $admin,
            'log_message' => $message,
            'log_file' => $file,
            'log_line' => $line,
            'log_event' => $event,
            'log_ip' => $_SERVER["REMOTE_ADDR"]
        ]);
        unset($log);
    }

    public static function getLogs($data){
        $log = DB::table('weibo_buglogs');
        $count = $log->count();
        if(isset($data['orderBy']) && isset($data['order'])){
            if($data['orderBy'] == 'id'){
                if($data['order'] == 'desc'){
                    $log = $log -> orderBy('log_id','desc');
                }
                else{
                    $log = $log -> orderBy('log_id','asc');
                }
            }
            else if($data['orderBy'] == 'event'){
                if($data['order'] == 'desc'){
                    $log = $log -> orderBy('log_event','desc');
                }
                else{
                    $log = $log -> orderBy('log_event','asc');
                }
            }
            else if($data['orderBy'] == 'ip'){
                if($data['order'] == 'desc'){
                    $log = $log -> orderBy('log_ip','desc');
                }
                else{
                    $log = $log -> orderBy('log_ip','asc');
                }
            }
            else{
                $log = $log -> orderBy('log_id','asc');
            }
        }
        else{
            $log = $log -> orderBy('log_id','asc');
        }

        if(isset($data['index']) && isset($data['limit'])){
            $log -> skip(($data['index'] - 1) * $data['limit']) -> take($data['limit']);
        }
        else{
            $log -> skip(1) -> take(20);
        }

        $log = $log -> get();

        if($log){
            $result = [];
            $result['total'] = $count;
            $result['logs'] = $log;
            return $result;
        }
        else{
            return null;
        }
    }
}